package com.yunyike.app.service;


import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.rop.annotation.IgnoreSignType;
import com.rop.annotation.NeedInSessionType;
import com.rop.annotation.ServiceMethod;
import com.rop.annotation.ServiceMethodBean;
import com.rop.response.Response;
import com.yunyike.app.constants.CommonConstant;
import com.yunyike.app.data.dto.CartDTO;
import com.yunyike.app.data.model.User;
import com.yunyike.app.data.service.ICartService;
import com.yunyike.app.data.service.IUserService;
import com.yunyike.app.redis.JedisManager;
import com.yunyike.app.request.CartRequest;
import com.yunyike.app.request.UserCartRequest;

@ServiceMethodBean(ignoreSign = IgnoreSignType.NO)
public class APPCartService {

    private Logger log = LoggerFactory.getLogger(APPCartService.class);
    
    @Autowired
   	private ICartService iCartService;
    
//  根据用户Id获取购物车列表										
	@ServiceMethod(method = "usercart.get", version = "1.0",needInSession=NeedInSessionType.NO)
	public Response getUserCart(UserCartRequest userCartRequest) {

		Response response = new Response();
		String shopperId = userCartRequest.getShopperId();
		String shopId = userCartRequest.getShopId();
		String msg = "";
		log.info("cart--getUserCart::shopperId=" + shopperId+",shopId="+shopId);
		try {
			User user = JedisManager.getObject(shopperId + CommonConstant.SHOPPER_CONNECT_USER, User.class);
			log.info("cart--getUserCart--jedis。get。user::user=" + user);

			if (user == null) {
				msg = "参数错误，userId获取不到用户";
				response.getResult().setResult("0");
			} else {

				CartDTO cartDTO = iCartService.getCart(user, shopId);
				response.getResult().setResult("1");
				Map<String, Object> data = new HashMap<String, Object>(1);
				if (cartDTO == null) {
					msg = "购物车为空";
				} else {
					msg = "购物车信息获取成功";
					data.put("CartDTO", cartDTO);
					log.info("++++++++++getTotalFreight"+cartDTO.getTotalFreight());
				}
				data.put("User", user);
				response.setData(data);
			}
			
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace();
		}
		response.getResult().setMessage(msg);
		log.info("cart--getUserCart::msg=" + msg);
		return response;
	}

//	添加到购物车						
	@ServiceMethod(method = "usercart.add", version = "1.0",needInSession=NeedInSessionType.NO)
	public Response addCart(CartRequest addCartRequest) {
		Response response = new Response();
		String shopperId = addCartRequest.getShopperId();
		String shopId = addCartRequest.getShopId();
		String merchandiseBarCode = addCartRequest.getMerchandiseBarCode();
		int qty = addCartRequest.getQty();
		
		String msg = "";
		log.info("cart--addCart::shopperId=" + shopperId+",shopId="+shopId+",merchandiseBarCode="+merchandiseBarCode+",qty="+qty);
		try {
			User user = JedisManager.getObject(shopperId + CommonConstant.SHOPPER_CONNECT_USER, User.class);
			log.info("cart--getUserCart--jedis。get。user::user=" + user);
			if (user == null) {
				msg = "参数错误，userId获取不到用户";
				response.getResult().setResult("0");
			} else {
				int addFlag = iCartService.addCart(user, shopId, merchandiseBarCode, qty);
				Map<String, Object> data = new HashMap<String, Object>(1);
				if (addFlag == 1) {
					msg = "添加购物车成功";
					response.getResult().setResult("1");
					CartDTO cartDTO = iCartService.getCart(user, shopId);
					data.put("CartDTO", cartDTO);
					data.put("User", user);
					response.setData(data);
				} else {
					msg = getReturnCode(addFlag);
					response.getResult().setResult("0");
				}
				response.getResult().setErrorCode(addFlag + "");
			}
			
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace();
		}
		response.getResult().setMessage(msg);
		log.info("cart--addCart::msg=" + msg);
		return response;
	}
	
	@ServiceMethod(method = "usercart.modify", version = "1.0",needInSession=NeedInSessionType.NO)
	public Response updateCart(CartRequest upCartRequest) {
		Response response = new Response();
		String shopperId = upCartRequest.getShopperId();
		String shopId = upCartRequest.getShopId();
		String merchandiseBarCode = upCartRequest.getMerchandiseBarCode();
		int qty = upCartRequest.getQty();
		
		String msg = "";
		log.info("cart--updateCart::shopperId=" + shopperId+",shopId="+shopId+",merchandiseBarCode="+merchandiseBarCode+",qty="+qty);
		try {
			User user = JedisManager.getObject(shopperId + CommonConstant.SHOPPER_CONNECT_USER, User.class);
			log.info("cart--getUserCart--jedis。get。user::user=" + user);
			if (user == null) {
				msg = "参数错误，userId获取不到用户";
				response.getResult().setResult("0");
			} else {
				int upFlag = 0;
				if (qty == 0) {
					upFlag = iCartService.removeCart(user, shopId, merchandiseBarCode);
				} else {
					upFlag = iCartService.updateCart(user, shopId, merchandiseBarCode, qty);
				}
				Map<String, Object> data = new HashMap<String, Object>(1);
				if (upFlag == 1) {
					msg = "操作成功";
					response.getResult().setResult("1");
					CartDTO cartDTO = iCartService.getCart(user, shopId);
					data.put("User", user);
					if (cartDTO != null){
						data.put("CartDTO", cartDTO);
					}else
						msg = "操作成功，购物车为空";
					response.setData(data);
				} else {
					msg = getReturnCode(upFlag);
					response.getResult().setResult("0");
				}
				response.getResult().setErrorCode(upFlag + "");
				response.setData(data);
			}
			
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace();
		}
		response.getResult().setMessage(msg);
		log.info("cart--updateCart::msg=" + msg);
		return response;
	}
	
	private String getReturnCode(int result){
		String returnCode = "";
		switch(result){
		// 添加成功
		case CommonConstant.NUMBER_1:
			returnCode = "操作成功";
			break;
		// 超重量
		case CommonConstant.NUMBER_2:
			returnCode = "超出重量";
			break;
		// 超数量
		case CommonConstant.NUMBER_3:
			returnCode = "超出数量";
			break;
		// 超金额
		case CommonConstant.NUMBER_4:
			returnCode = "超出金额";
			break;
		// 添加失败
		default:
			returnCode ="操作失败";
			break;	
		}
		return returnCode;
	}
	
	
}

